package com.corticon.client.model;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.corticon.client.util.ConvertSpecialUtil;

import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class NavigatePageOutput extends DialogOutput {
	private PageJsonOutput page;
	private List<QuestionNavigateOutput> question;	

	public NavigatePageOutput() {

	}
	private ConvertSpecialUtil covertSpecialUtil = new ConvertSpecialUtil();
	public PageJsonOutput getPage() {
		return page;
	}

	public void setPage(PageJsonOutput page) {
		this.page = page;
	}

	public List<QuestionNavigateOutput> getQuestion() {
		return question;
	}

	public void setQuestion(List<QuestionNavigateOutput> question) {
		this.question = question;
	}

	public JSONObject toJson() {
		return JSONObject.fromObject(this);
	}
	
	public NavigatePageOutput toBean(String obj) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		NavigatePageOutput navigatePageOutput = (NavigatePageOutput) JSONObject.toBean(JSONObject.fromObject(obj), this.getClass());
		List<QuestionNavigateOutput> questions = navigatePageOutput.getQuestion();
		List<GeneralMessage> messages = navigatePageOutput.getErrorMessage();
		JSONArray jsonArrayMsg = JSONArray.fromObject(messages);
		navigatePageOutput.setErrorMessage(covertMessage(jsonArrayMsg.toString()));
		JSONArray jsonArray = JSONArray.fromObject(questions);		
		List newQuestions = this.covertQuestionBean(jsonArray.toString());
		navigatePageOutput.setQuestion(newQuestions);
		return navigatePageOutput;
	}
	public List<GeneralMessage> covertMessage(String jsonStr){
		List<GeneralMessage> messages = new ArrayList();
		Collection<MorphDynaBean> mbs = JSONArray.toCollection(JSONArray.fromObject(jsonStr));
		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			GeneralMessage msg = new GeneralMessage();
			msg.setCode(mb.get("code").toString());
			msg.setQuestionId(Integer.valueOf(mb.get("questionId")==null?null:mb.get("questionId").toString()));
			msg.setText(mb.get("text").toString());
			messages.add(msg);
		}	
		
		return messages;
	}
	public List<QuestionNavigateOutput> covertQuestionBean(String jsonStr) {
		List<QuestionNavigateOutput> returnObjects = new ArrayList();
		Collection<MorphDynaBean> mbs = JSONArray.toCollection(JSONArray.fromObject(jsonStr));
		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			QuestionNavigateOutput newObj = new QuestionNavigateOutput();
			String answerValue = "";
			Object answerObj = mb.get("answerValue");
			List answerArray = (List) answerObj;
			for (int i = 0; i < answerArray.size(); i++) {
				answerValue += mb.get("answerValue", i) + ",";
			}
			newObj.setAnswerValue(answerValue.split(","));
			newObj.setDataType(mb.get("dataType").toString());
			newObj.setHelpText(mb.get("helpText").toString());
			newObj.setMaxResponses(Integer.valueOf(mb.get("maxResponses").toString()));
			newObj.setName(mb.get("name").toString());
			newObj.setResponseEntry(covertResponseEntryBean(mb.get("responseEntry")));
			newObj.setMaxResponseValue(mb.get("maxResponseValue").toString());
			newObj.setMinResponseValue(mb.get("minResponseValue").toString());
			newObj.setQuestionId(Integer.valueOf(mb.get("questionId").toString()));
			newObj.setQuestionPageId(Integer.valueOf(mb.get("questionPageId").toString()));
			newObj.setRefId(mb.get("refId").toString());
			newObj.setResponseType(mb.get("responseType").toString());
			newObj.setRows(mb.get("rows").toString());
			newObj.setSequence(mb.get("sequence").toString());
			newObj.setText(mb.get("text").toString());
			newObj.setUiControl(mb.get("uiControl").toString());
			newObj.setVersion(Integer.valueOf(mb.get("version").toString()));
			newObj.setWidth(mb.get("width").toString());
			newObj.setText(covertSpecialUtil.covertSpecialChar(newObj.getText()));			
			newObj.setHelpText(covertSpecialUtil.covertSpecialChar(newObj.getHelpText()));			
			returnObjects.add(newObj);
		}
		return returnObjects;
	}

	public List<ResponseEntryApiOutput> covertResponseEntryBean(Object morphDynaObj) {
		List<ResponseEntryApiOutput> returnObjects = new ArrayList();
		Collection<MorphDynaBean> mbs = JSONArray.toCollection(JSONArray.fromObject(morphDynaObj));
		for (Iterator<MorphDynaBean> rsIterator = mbs.iterator(); rsIterator.hasNext();) {
			MorphDynaBean mb = rsIterator.next();
			ResponseEntryApiOutput newObj = new ResponseEntryApiOutput();
			newObj.setLabel(mb.get("label").toString());
			newObj.setResponseEntryId(Integer.valueOf(mb.get("responseEntryId").toString()));
			newObj.setSequence(mb.get("sequence").toString());
			newObj.setValue(mb.get("value").toString());
			newObj.setLabel(covertSpecialUtil.covertSpecialChar(newObj.getLabel()));
			returnObjects.add(newObj);
		}
		return returnObjects;
	}
}
